package com.da.system;

import java.io.InputStream;
import java.util.Properties;

import javax.annotation.Resource;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

import com.da.finance.DaFinanceStatementService;
import com.da.landlord.AuthUserService;
import com.da.landlord.DaRoomService;
import com.espirit.eap.manager.SystemConfig;
import com.espirit.eap.manager.SystemConfigService;
import com.espirit.eap.manager.TaskScheduler;
import com.espirit.eap.manager.TaskSchedulerService;
import com.espirit.eap.util.JsonService;
import com.googlecode.cswish.struts.hibernate.DynamicModelManager;
import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.ra.landlord.RoomService;
import com.ra.system.RaPrivilegeInitService;

@Service
public class DaDataInitService {


	@Resource
	private DynamicModelManager dynamicModelManager;
	
	@Resource
	private GenericService genericService;
	
	@Resource
	private JsonService jsonService;
	
	@Resource
	private AuthUserService authService;
	
	@Resource
	private AuthDataService authData;
	
	@Resource
	private RaPrivilegeInitService raPrivilegeInitService;
	
	@Resource
	private TaskSchedulerService taskSchedulerService;
	
	@Resource
	private DaRoomService daRoomService;
	
	@Resource
	private RoomService roomService;
	
	private SystemConfigService systemConfigService;
	
	private SystemConfigService getSystemConfigService() {
		if (systemConfigService == null) {
			systemConfigService = ObjectFactory.getInstance().getBean(SystemConfigService.class, false);
		}
		return systemConfigService;
	}
	
	/**
	 * 按模型修改表结构
	 */
	public boolean refreshDb() {
		dynamicModelManager.refreshDbSchema(false, true, false, false);
		return true;
	}
	
	public boolean initData() {
		
		jsonService.syncPropertyToSystemConfig();
		
		syncRaJsonPropertyToSystemConfig();

		raPrivilegeInitService.initAllPrivilege();

		authService.authDataInit();
		
		//daRoomService.correctRoomAgreement(); //修正冗余字段agreement的数据
		// 为vc版产生财务
		createTaskScheduler("财务产生", DaFinanceStatementService.class, "autoCreateFinanceStatementForVC", "0 15 2 * * ?", "自动扫描vc未产生的fee, 每晚2点15分更新");
		
		createTaskScheduler("大集中适配vc", RoomService.class, "adaptForVC", "0 0 3 * * ?", "大集中适配vc, 每晚3点00分更新");
		return true;
	}
	
	public void syncRaJsonPropertyToSystemConfig() {
		String jsonPropertyFile = "/ra-json.properties";
		InputStream input = null;
		try {
			input = this.getClass().getResourceAsStream(jsonPropertyFile);
			Properties properties = new Properties();
			if (input != null) {
				properties.load(input);
				
				for (Object key : properties.keySet()) {
					Object value = properties.get(key);
					
					if (key != null && value != null) {
						SystemConfig systemConfig = new SystemConfig();
						systemConfig.setName((String)key);
						systemConfig.setValue((String)value);
						systemConfig.setType(SystemConfig.TYPE_JSON);
						getSystemConfigService().mergeByTypeName(systemConfig, true);
					}
				}
			}
			
		} catch (Exception ex) {
		} finally {
			IOUtils.closeQuietly(input);
		}
	}
	

	private void createTaskScheduler(String name, Class clazz, String method, String cond, String description) {
		TaskScheduler taskScheduler = new TaskScheduler();
		taskScheduler.setCommand(clazz.getName() + "."	+ method);
		taskScheduler.setActive(true);
		if (name != null) {
			taskScheduler.setName(name);
		} else {
			taskScheduler.setName(method);
		}
		taskScheduler.setDescription(description);
		taskScheduler.setCond(cond);
		taskScheduler.setOnlyMaster(true);
		taskSchedulerService.checkAndAdd(taskScheduler);
	}
}